//
//  OwnScoreView.m
//  Quiz
//
//  Created by AnhLH-Oriolus on 31/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "OwnScoreView.h"


@implementation OwnScoreView


@synthesize arrScore, arrName;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		UIImageView *v = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
		[v setImage:[UIImage imageNamed:@"images/bkgrClue.png"]];
		[v setUserInteractionEnabled:YES];
		[self addSubview:v];
		[v release];
		
		lblOwnScore = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 45)]; 
		[lblOwnScore setBackgroundColor:[UIColor clearColor]];
		lblOwnScore.textAlignment = UITextAlignmentCenter;
		lblOwnScore.textColor = [UIColor whiteColor];
		lblOwnScore.font = [UIFont fontWithName:@"Arial" size:18];
		lblOwnScore.text = @"Own's Score";
		[self addSubview:lblOwnScore];
		[lblOwnScore release];
		
		_tableView = [[UITableView alloc] initWithFrame:CGRectMake(10, 40, 300, 150) style:UITableViewStylePlain];
		[_tableView setBackgroundColor:[UIColor clearColor]];
		_tableView.rowHeight = 30;
		_tableView.dataSource = self;
		_tableView.delegate = self;
		[self addSubview:_tableView];
		[_tableView release];
    }
    return self;
}

-(void)setLabelTitle:(int)mode{
	if(mode == 0){
		lblOwnScore.text = @"Own's Score";
	}
	if(mode == 1){
		lblOwnScore.text = @"Multiplayer's Score";
	}
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


-(void)setDataWithPlayerName:(NSMutableArray *)arrNames withArrScore:(NSMutableArray *)arrScores{
	
	arrScore = [arrScores retain];
	arrName = [arrNames retain];
	
	[_tableView reloadData];
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	if([arrName count] > 10)
		return 10;
	
	return [arrName count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	ScoreCellView* cell = nil;

	cell = (ScoreCellView *)[tableView dequeueReusableCellWithIdentifier:@"ownScore"];
	if(!cell){
		cell = [[[ScoreCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ownScore" withMode:0] autorelease];
	}
	
	NSString *nameFormat = [NSString stringWithFormat:@"%d. %@", indexPath.row+1, [arrName objectAtIndex:indexPath.row]];
	[cell setPlayerName:nameFormat withScore:[[arrScore objectAtIndex:indexPath.row] intValue]];
	
	return cell;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	if(arrName){
		[arrName removeAllObjects];
		arrName = nil;
	}
	if(arrScore){
		[arrScore removeAllObjects];
		arrScore = nil;
	}
	
    [super dealloc];
}


@end
